package com.auth.cloud.user.controller;

import com.auth.cloud.common.entity.User;
import com.auth.cloud.common.result.Result;
import com.auth.cloud.user.dto.UserCreateRequest;
import com.auth.cloud.user.dto.UserUpdateRequest;
import com.auth.cloud.user.dto.UserQueryRequest;
import com.auth.cloud.user.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

/**
 * 用户控制器
 *
 * @author auth-cloud
 * @since 2024-01-01
 */
@Tag(name = "用户管理", description = "用户管理相关接口")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Validated
public class UserController {

    private final UserService userService;

    @Operation(summary = "分页查询用户", description = "根据条件分页查询用户列表")
    @GetMapping("/page")
    public Result<IPage<User>> page(@Valid UserQueryRequest request) {
        IPage<User> page = userService.page(request);
        return Result.success(page);
    }

    @Operation(summary = "根据ID查询用户", description = "根据用户ID查询用户详情")
    @GetMapping("/{id}")
    public Result<User> getById(@Parameter(description = "用户ID") @PathVariable @NotNull Long id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    @Operation(summary = "创建用户", description = "创建新用户")
    @PostMapping
    public Result<Void> create(@Valid @RequestBody UserCreateRequest request) {
        userService.create(request);
        return Result.success("创建成功");
    }

    @Operation(summary = "更新用户", description = "更新用户信息")
    @PutMapping("/{id}")
    public Result<Void> update(@Parameter(description = "用户ID") @PathVariable @NotNull Long id,
                               @Valid @RequestBody UserUpdateRequest request) {
        userService.update(id, request);
        return Result.success("更新成功");
    }

    @Operation(summary = "删除用户", description = "根据ID删除用户")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@Parameter(description = "用户ID") @PathVariable @NotNull Long id) {
        userService.delete(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "启用用户", description = "启用指定用户")
    @PutMapping("/{id}/enable")
    public Result<Void> enable(@Parameter(description = "用户ID") @PathVariable @NotNull Long id) {
        userService.enable(id);
        return Result.success("启用成功");
    }

    @Operation(summary = "禁用用户", description = "禁用指定用户")
    @PutMapping("/{id}/disable")
    public Result<Void> disable(@Parameter(description = "用户ID") @PathVariable @NotNull Long id) {
        userService.disable(id);
        return Result.success("禁用成功");
    }

    @Operation(summary = "重置密码", description = "重置用户密码")
    @PutMapping("/{id}/reset-password")
    public Result<String> resetPassword(@Parameter(description = "用户ID") @PathVariable @NotNull Long id) {
        String newPassword = userService.resetPassword(id);
        return Result.success("密码重置成功", newPassword);
    }
}
